﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using Common;
using CrystalDecisions.CrystalReports.Engine;

namespace CNCSoft.ErpApps.TMS.longACSS
{
    public partial class frmReportKetQuaThiLopWithType : DevExpress.XtraEditors.XtraForm
    {
        SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        DataSet ds = new DataSet();

        public frmReportKetQuaThiLopWithType()
        {
            InitializeComponent();
        }

        private void frmReportKetQuaThiLopWithType_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_Xem_Click(object sender, EventArgs e)
        {
            if (Validation())
                PrintData("myrpt_KetQuaThiCuaLopWithType", searchLookUpEdit_KhoaHoc.EditValue.ToString(), radioGroup.EditValue.ToString());
        }

        #region "coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                LoadKhoaHoc();
                searchLookUpEdit_KhoaHoc.Properties.DataSource = ds.Tables["TableKhoaHoc"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadKhoaHoc()
        {
            try
            {
                if (ds.Tables["TableKhoaHoc"] != null)
                    ds.Tables["TableKhoaHoc"].Clear();
                string sql = @"select * from DM_KHOAHOC";
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                da.Fill(ds, "TableKhoaHoc");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void PrintData(string namereport, string makhoahoc, string type)
        {
            try
            {
                btn_Xem.Enabled = false;
                //Load Report
                ReportDocument rpt = new ReportDocument();
                if (GlobalVariable.WSInfo.CompanyID.ToLower() == "english")
                    rpt.Load(Application.StartupPath + "\\Report\\" + namereport + ".rpt");
                //Set Connection
                PublicFunction.SetDBLogonForReport(rpt);
                //Set parameters
                PublicFunction.SetParameter(rpt, "@makhoahoc", makhoahoc);
                PublicFunction.SetParameter(rpt, "@type", type);
                //View report
                frmPrintPreview view = new frmPrintPreview(rpt);
                view.ShowDialog();
                view.Dispose();
                rpt.Dispose();
                btn_Xem.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private bool Validation()
        {
            try
            {
                if (searchLookUpEdit_KhoaHoc.EditValue == null)
                {
                    MessageBox.Show("Bạn chưa chọn lớp");
                    return false;
                }
                else if (searchLookUpEdit_KhoaHoc.EditValue.ToString() != "")
                {
                    return true;
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion
    }
}